#include "invader.h"

namespace sprites
{
	Invader::Invader(Vector3D _pos, MatterType _type, float _boundRadius, float scale)
		: Sprite(_pos, _type, _boundRadius, scale), bullet(0), bulletShot(false), angleSign(1)
	{
		leftArm = new ParentNode();
		rightArm = new ParentNode();
		build();
	}
	Invader::Invader(Vector3D _pos, MatterType _type, float _boundRadius)
		: Sprite(_pos, _type, _boundRadius, 1.0f), bullet(0), bulletShot(false), angleSign(1)
	{
		build();
	}
	Invader::~Invader() 
	{
	    root->destroy();
	}

	//builds object's representation
	void Invader::buildLeftMouth( ParentNode* _parent )
	{
		//-----
		Cube* mainSquare = new Cube(1.0f, 1.0f, 0.5f, 0xFF010101);

		//-----
		Cube* partMouth  = new Cube(0.5f, 0.5f, 0.5f, 0xFF010101);
		partMouth->setTranslation(Vector3D(-0.25f, -0.75f, 0.0f));

		//-----
		Cube* mouth = new Cube(1.0f, 0.5f, 0.5f, 0xFF010101);
		mouth->setTranslation(Vector3D(0.5f, -1.25f, 0.0f));

		//-----
		// add the children
		//-----
		_parent->addChild(mainSquare);
		_parent->addChild(partMouth);
		_parent->addChild(mouth);
	}

	void Invader::buildRightMouth( ParentNode* _parent ){
		//-----
		Cube* mainSquare = new Cube(1.0f, 1.0f, 0.5f, 0xFF010101);

		//-----
		Cube* partMouth = new Cube(0.5f, 0.5f, 0.5f, 0xFF010101);
		partMouth->setTranslation(Vector3D(0.25f, -0.75f, 0.0f));
		
		//-----
		Cube* mouth = new Cube(1.0f, 0.5f, 0.5f, 0xFF010101);
		mouth->setTranslation(-0.5f, -01.25f, 0.0f);

		//-----
		// add the children
		//-----
		_parent->addChild(mainSquare);
		_parent->addChild(partMouth);
		_parent->addChild(mouth);
	}
	
	void Invader::buildLeftEar(ParentNode* _parent){

		//-----
		Cube* bottomEar = new Cube(1.0f, 0.5f, 0.5f, 0xFF010101);

		//-----
		Cube* midEar = new Cube(0.5f, 0.5f, 0.5f, 0xFF010101);
		midEar->setTranslation(Vector3D(0.25f, 0.5f, 0.0f));

		//-----
		Cube* topEar = new Cube(0.5f, 0.5f, 0.5f, 0xFF010101);
		topEar->setTranslation(-0.25f, 1.0f, 0.0f);

		//-----
		// add the children
		//-----
		_parent->addChild(bottomEar);
		_parent->addChild(midEar);
		_parent->addChild(topEar);
	}
	
	void Invader::buildRightEar(ParentNode* _parent){
		//-----
		Cube* bottomEar = new Cube(1.0f, 0.5f, 0.5f, 0xFF010101);

		//-----
		Cube* midEar = new Cube(0.5f, 0.5f, 0.5f, 0xFF010101);
		midEar->setTranslation(Vector3D(-0.25f, 0.5f, 0.0f));

		Cube* topEar = new Cube(0.5f, 0.5f, 0.5f, 0xFF010101);
		topEar->setTranslation(0.25f, 1.0f, 0.0f);

		//-----
		// add the children
		//-----
		_parent->addChild(bottomEar);
		_parent->addChild(midEar);
		_parent->addChild(topEar);
	}
	
	void Invader::buildLeftArm(ParentNode* _parent){

		//-----
		Cube* topArm = new Cube(1.0f, 0.5f, 0.5f, 0xFF010101);

		//-----
		Cube* midArm = new Cube(1.0f, 0.5f, 0.5f, 0xFF010101);
		midArm->setTranslation(Vector3D(-0.5f, -0.5f, 0.0f));

		//-----
		Cube* bottomArm = new Cube(0.5f, 1.0f, 0.5f, 0xFF010101);
		bottomArm->setTranslation(Vector3D(-0.75f, -1.25f, 0.0f));

		//-----
		// add the children
		//-----
		_parent->addChild(topArm);
		_parent->addChild(midArm);
		_parent->addChild(bottomArm);
	}
	
	void Invader::buildRightArm(ParentNode* _parent){
		
		//-----
		Cube* topArm = new Cube(1.0f, 0.5f, 0.5f, 0xFF010101);

		//-----
		Cube* midArm = new Cube(1.0f, 0.5f, 0.5f, 0xFF010101);
		midArm->setTranslation(Vector3D(0.5f, -0.5f, 0.0f));

		//-----
		Cube* bottomArm = new Cube(0.5f, 1.0f, 0.5f, 0xFF010101);
		bottomArm->setTranslation(Vector3D(0.75f, -1.25f, 0.0f));

		//-----
		// add the children
		//-----
		_parent->addChild(topArm);
		_parent->addChild(midArm);
		_parent->addChild(bottomArm);
	}


	BindingSphere Invader::getBindingSphere()
	{
		bound.moveTo(parent.getTranslation());
		return bound;
	}


	std::vector<BindingSphere> Invader::getBindingShapes()
	{
		bound.moveTo(parent.getTranslation());
		bindingShapes = vector<BindingSphere>();
		bindingShapes.push_back(bound);
		return bindingShapes;
	}

	void Invader::build()
	{
		//currentNode = 0;
		int nodePosition = 0;
		int color = 0xFF010101;
		float tmpVector[] = { 0.0f, 0.0f, -5.0f };
		

		Cube* square = new Cube(1.5f, 2.0f, 0.5f, color);
		
		root->setShape(square);
		
		ParentNode* leftBodyMouth = new ParentNode();
		root->addChild(leftBodyMouth);

		buildLeftMouth(leftBodyMouth);
		leftBodyMouth->setTranslation(Vector3D(-1.25f, -0.5f, 0.0f));
		

		ParentNode* rightBodyMouth = new ParentNode();
		root->addChild(rightBodyMouth);
		
		buildRightMouth(rightBodyMouth);
		rightBodyMouth->setTranslation(Vector3D(1.25f, -0.5f, 0.0f));
		
		ParentNode* leftEar = new ParentNode();
		root->addChild(leftEar);

		buildLeftEar(leftEar);
		leftEar->setTranslation(Vector3D(-1.25f, 0.75f, 0.0f));
		
		ParentNode* rightEar = new ParentNode();
		root->addChild(rightEar);

		buildRightEar(rightEar);
		rightEar->setTranslation(Vector3D(1.25f, 0.75f, 0.0f));
		
		leftArm = new ParentNode();
		root->addChild(leftArm);

		buildLeftArm(leftArm);
		leftArm->setTranslation(Vector3D(-1.75f, 0.25f, 0.0f));
		
		rightArm = new ParentNode();
		root->addChild(rightArm);

		buildRightArm(rightArm);
		rightArm->setTranslation(Vector3D(1.75f, 0.25f, 0.0f));
	}

	//for collision handling
	void Invader::handleCollision(MatterType oType, BindingSphere sphere)
	{
		switch(oType)
		{
		case collisions::BARRIER:
			break;
		case collisions::SHIPBULLET :
			dead = true;
			break;
		case collisions::INVADERBULLET :
			break;
		case collisions::INVADER :
			break;
		case collisions::SHIP :
			break;
		}
	}

	//returns a pointer to the bullet that is shot
	Bullet* Invader::shoot()
	{
		if(!bulletShot)//only shoot if your bullet has been destroyed
		{
			if(bullet == 0)
			{
				bullet = new Bullet(parent.getTranslation(), collisions::INVADERBULLET, 0.05f, Vector3D(0, -0.15f, 0), scale);
			}
			else
			{
				bullet->setPosition(parent.getTranslation());
				bullet->revive();
			}
			bulletShot = true;
		}
		return bullet;
	}

	//used by the invader controller to move the invader
	void Invader::moveLeft()
	{
		Vector3D axis = Vector3D(0, 0, 1);
		float angle = 0;
		leftArm->getAxisAngle(&axis, &angle);
		leftArm->setAxisAngle(axis, -(float)((int)(angle + 45)%90));
		rightArm->setAxisAngle(axis, (float)((int)(angle + 45)%90));

		parent+=Vector3D(-0.8f, 0.0f, 0.0f);
	}
	void Invader::moveRight()
	{
		Vector3D axis = Vector3D(0, 0, 1);
		float angle = 0;
		leftArm->getAxisAngle(&axis, &angle);
		leftArm->setAxisAngle(axis, -(float)((int)(angle + 45)%90));
		rightArm->setAxisAngle(axis, (float)((int)(angle + 45)%90));

		parent+=Vector3D(0.8f, 0.0f, 0.0f);
	}
	void Invader::moveDown()
	{
		parent+=Vector3D(0.0f, -1.0f, 0.0f);
	}

	//called by the game to draw the invader
	void Invader::draw()
	{
		if(!dead)
		{
			parent.draw();
		}
		if(bulletShot && !bullet->isDead())
		{
			//do nothing
		}
		else
		{
			bulletShot = false;
		}
	}
	bool Invader::isShooting()
	{
		return bulletShot;
	}
}